package com.pumpkin.iplocationdemo.component;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class IpInfo {

    @GetMapping("/test")
    public String getIpInfo() throws IOException {
        Document document = Jsoup.connect("https://2022.ip138.com/").get();
        String ip = document.selectXpath("/html/body/p[1]/a[1]").get(0).text();
        String ipInfo = document.selectXpath("/html/body/p[1]").get(0).text();
        String city = ipInfo.substring(ipInfo.lastIndexOf("来自：") + 3);
        System.out.println(ip);
        System.out.println(city);

        return ip + " 您来自：" + city;
    }
}
